package nl.jjsoft.airlock.view.presenters
{
	import mx.managers.PopUpManager;
	
	import nl.jjsoft.airlock.controller.SettingsController;
	import nl.jjsoft.airlock.domain.Model;
	import nl.jjsoft.airlock.domain.Session;
	import nl.jjsoft.airlock.utils.Constants;
	import nl.jjsoft.airlock.utils.ResourceFactory;
	import nl.jjsoft.airlock.view.Settings;
	import nl.jjsoft.airlock.vo.PasswordPolicyVo;
	
	import org.swizframework.storage.ISharedObjectBean;
	
	public class SettingsPresenter extends AbstractPresenter
	{
		[Inject]
		public var model:Model;
		
		[Inject]
		public var session:Session;
		
		[Inject]
		public var settingsController:SettingsController;
		
		[Inject]
		public var so:ISharedObjectBean;
		
		[Inject]
		public var resourceFactory:ResourceFactory;
		
		public function SettingsPresenter()
		{
			super();
		}
		
		public function closeSettings(view:Settings):void {
			PopUpManager.removePopUp(view);	
		}
		
		public function saveSettings(settings:Settings):void {
			var passwordPolicy:PasswordPolicyVo = new PasswordPolicyVo();
			passwordPolicy.id = model.passwordPolicy.id;
			passwordPolicy.length = settings.cPasswordLength.value;
			passwordPolicy.useDigits = settings.cDigits.selected;
			passwordPolicy.useLowerCase = settings.cLowerCase.selected;
			passwordPolicy.useUpperCase = settings.cUpperCase.selected;
			passwordPolicy.useSymbols = settings.cSymbols.selected;
			if (settingsController.saveSettings(passwordPolicy)) {
				model.passwordPolicy = passwordPolicy;
			}
			
			closeSettings(settings);
		}
		
		public function changeLocale(settings:Settings):void {
			var locale:String = settings.languages.selectedItem.data;
			so.setString(Constants.LOCALE, locale);
			resourceFactory.changeLocale(locale);
			session.currentLocale = locale;
			closeSettings(settings);
		}
	}
}